package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppGiftReward;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 礼物打赏表
 */
@Repository
public class AppGiftRewardDao extends BaseDao<AppGiftReward> {

    /**
     * 查询列表
     */
    public List<AppGiftReward> list(Map<String,Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_gift_reward where 1=1  ");
        if(StringEx.isNotNull(paraMap.get("startDate"))){
            sbStr.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  >=  ? ");
            paramList.add(paraMap.get("startDate"));
        }
        if(StringEx.isNotNull(paraMap.get("endDate"))){
            sbStr.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  <=  ? ");
            paramList.add(paraMap.get("endDate"));
        }
        if(StringEx.isNotNull(paraMap.get("giftname"))){
            sbStr.append(" and  giftname  like ? ");
            paramList.add("%"+paraMap.get("giftname")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("dsnick"))){
            sbStr.append(" and dsnick like ? ");
            paramList.add("%"+paraMap.get("dsnick")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("dsphone"))){
            sbStr.append(" and dsphone = ? ");
            paramList.add(paraMap.get("dsphone"));
        }
        if(StringEx.isNotNull(paraMap.get("nick"))){
            sbStr.append(" and nick like  ? ");
            paramList.add("%"+paraMap.get("nick")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("phone"))){
            sbStr.append(" and phone = ? ");
            paramList.add(paraMap.get("phone"));
        }
        if(StringEx.isNotNull(paraMap.get("roomcode"))){
            sbStr.append(" and roomcode = ? ");
            paramList.add(paraMap.get("roomcode"));
        }
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppGiftReward.class,splitPage);
    }


    /**
     * 详情
     */
    public AppGiftReward get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_gift_reward where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppGiftReward.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppGiftReward haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id <= 0) {
            return;
        }
        String sql = "delete  from app_gift_reward where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }
    public List<AppGiftReward> findListByTime(Map<String, Object> parMap, SplitPageBean splitPageBean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * from app_gift_reward p  where 1=1 ");
        if (StringEx.isNotNull(parMap.get("nick"))) {//被打赏人
            sbStr.append(" and p.nick like ? ");
            paramList.add("%" + parMap.get("nick") + "%");
        }
        if (StringEx.isNotNull(parMap.get("dsnick"))) {//打赏人
            sbStr.append(" and p.dsnick like ? ");
            paramList.add("%" + parMap.get("dsnick") + "%");
        }

        if (StringEx.isNotNull(parMap.get("roomcode"))) {
            sbStr.append(" and p.roomcode = ? ");
            paramList.add(parMap.get("roomcode"));
        }
        if (!StringEx.isNull(parMap.get("startDate"))) {
            sbStr.append(" and DATE_FORMAT(p.createtime) >=  ? ");
            paramList.add(parMap.get("startDate"));
        }
        if (!StringEx.isNull(parMap.get("endDate"))) {
            sbStr.append(" and DATE_FORMAT(p.createtime) <=  ? ");
            paramList.add(parMap.get("endDate"));
        }
        if (!StringEx.isNull(parMap.get("userid"))) {
            sbStr.append(" and p.userid = ?  ");
            paramList.add(parMap.get("userid"));
        }
        if (!StringEx.isNull(parMap.get("rewardid"))) {
            sbStr.append(" and p.rewardid = ?  ");
            paramList.add(parMap.get("rewardid"));
        }
        sbStr.append(" order by p.createtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppGiftReward.class, splitPageBean);
    }
    public Long findListByInfo(Map<String, Object> parMap) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select IFNULL(sum(money),0) from app_gift_reward p  where 1=1 ");
        if (StringEx.isNotNull(parMap.get("nick"))) {//被打赏人
            sbStr.append(" and p.nick like ? ");
            paramList.add("%" + parMap.get("nick") + "%");
        }
        if (StringEx.isNotNull(parMap.get("dsnick"))) {//打赏人
            sbStr.append(" and p.dsnick like ? ");
            paramList.add("%" + parMap.get("dsnick") + "%");
        }

        if (StringEx.isNotNull(parMap.get("roomcode"))) {
            sbStr.append(" and p.roomcode = ? ");
            paramList.add(parMap.get("roomcode"));
        }
        if (StringEx.isNotNull(parMap.get("rewardid"))) {
            sbStr.append(" and p.rewardid = ? ");
            paramList.add(parMap.get("rewardid"));
        }
        if (!StringEx.isNull(parMap.get("startDate"))) {
            sbStr.append(" and DATE_FORMAT(a.createtime) >=  ? ");
            paramList.add(parMap.get("startDate"));
        }
        if (!StringEx.isNull(parMap.get("endDate"))) {
            sbStr.append(" and DATE_FORMAT(a.createtime) <=  ? ");
            paramList.add(parMap.get("endDate"));
        }
        String sql = sbStr.toString();
        return this.queryListCountLong(sql, paramList);
    }



    /**
     * 更新被打赏人头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateImg(Integer userid,String pic){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(pic)){
            return;
        }
        String sql = " UPDATE app_gift_reward SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新被打赏人昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateNick(Integer userid,String nick){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(nick)){
            return;
        }
        String sql = " UPDATE app_gift_reward SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }


    /**
     * 更新打赏人头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateDsImg(Integer userid,String pic){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(pic)){
            return;
        }
        String sql = " UPDATE app_gift_reward SET dspic = ?  WHERE rewardid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新打赏人昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateDsNick(Integer userid,String nick){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(nick)){
            return;
        }
        String sql = " UPDATE app_gift_reward SET dsnick = ?  WHERE rewardid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    public List<AppGiftReward> list(Map<String,Object> paraMap) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_gift_reward where 1=1  ");

        if(paraMap.containsKey("today") && StringEx.isNotNull(paraMap.get("today"))) {
            sbStr.append(" and to_days(createtime) = to_days(now())");
        }
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppGiftReward.class);
    }
}
